from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.common.keys import Keys


class Send_mail:
    def __init__(self):
        # 打开浏览器，请求目标网址
        self.driver = webdriver.Chrome()
        self.driver.get('https://mail.163.com/')

    def login_163(self):
        # # 打开浏览器，请求目标网址
        # driver = webdriver.Chrome()
        # driver.get('https://mail.163.com/')

        # 添加登录需要的cookie
        cookies = [
            {
                'name': 'NTES_SESS',
                'value': 'IgyBXZ1EGnWUWecDEgj5h6u5ltNN8J4W8PK4b6EvVjJNmVb8mPEkn1d8V3CrYfqafLr8.Ox0rIS5nvWp_wIiJ8VH1ywn5nnVjYjBiGDypRegDfcBJxWdxoCKr4Pqoj17sdSYABXYed73Yt0CceQg68cgxjQzmTzMZ44cMsfRJHmb814zlLASer94A9AfcywUTI_2lqMjM87shLJ0UAnghvBcD'
            },
            {
                'name': 'MAIL_ENTRY_INFO',
                'value': '1|0|mail163|mail163_letter|59.173.53.73|d3c7cfc5e9025a6b299737db562996ad_v1|'
            },
            {
                'name': 'MAIL_ENTRY_CS',
                'value': '35ab1b52045be38b9db94c5a282122e4'
            },
            {
                'name': 'Coremail',
                'value': '606afab8ea379%sCTrQYRtPvBpjkbBhrttaWPaQKKJFcqq%g1a128.mail.163.com'
            },
            {
                'name': 'cm_last_info',
                'value': 'dT1oangxOTc1NTAxMjgyJTQwMTYzLmNvbSZkPWh0dHBzJTNBJTJGJTJGbWFpbC4xNjMuY29tJTJGanM2JTJGbWFpbi5qc3AlM0ZzaWQlM0RzQ1RyUVlSdFB2QnBqa2JCaHJ0dGFXUGFRS0tKRmNxcSZzPXNDVHJRWVJ0UHZCcGprYkJocnR0YVdQYVFLS0pGY3FxJmg9aHR0cHMlM0ElMkYlMkZtYWlsLjE2My5jb20lMkZqczYlMkZtYWluLmpzcCUzRnNpZCUzRHNDVHJRWVJ0UHZCcGprYkJocnR0YVdQYVFLS0pGY3FxJnc9aHR0cHMlM0ElMkYlMkZtYWlsLjE2My5jb20mbD0tMSZ0PS0xJmFzPXRydWU='
            }
        ]

        # 添加cookie
        self.driver.add_cookie(cookies[0])
        self.driver.add_cookie(cookies[1])
        self.driver.add_cookie(cookies[2])
        self.driver.add_cookie(cookies[3])
        self.driver.add_cookie(cookies[4])

        # 刷新浏览器
        self.driver.refresh()

    def send_mail(self):
        # 定位写信并点击
        self.driver.find_element(By.ID, '_mail_component_149_149').click()
        # 定位给自己写一封信按钮
        # btn1=self.driver.find_element(By.XPATH,'//*[@id="1652701848765_list"]/div/div[1]/ul/li/a')
        # btn1.click()

        # 点击收件人，输入
        addr = self.driver.find_element(By.CLASS_NAME, 'nui-editableAddr-ipt')
        addr.send_keys('jh13552193364@163.com')

        # 定位主题，输入
        self.driver.find_elements(By.CLASS_NAME, 'nui-ipt-input')[2].send_keys('这是主题')
        # title1.send_keys('发邮件操作')

        # iframe切换到邮件正文
        inner = self.driver.find_element(By.CLASS_NAME, 'APP-editor-iframe')
        frame1 = self.driver.switch_to.frame(inner)

        # 定位正文输入内容
        write = self.driver.find_element(By.CLASS_NAME, 'nui-scroll')
        write.send_keys('这是邮件的正文内容')

        time.sleep(3)
        self.driver.switch_to.default_content()
        send_btn = self.driver.find_element(By.ID,'_mail_button_8_276')
        send_btn.click()


if __name__ == '__main__':
    send = Send_mail()
    send.login_163()
    send.send_mail()
